﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using Model;

public partial class GroupBuying_BuyUserList : BasePage
{
    GroupBuyingUserBO mbo = new GroupBuyingUserBO();
    GroupBuyingBO gbo = new GroupBuyingBO();
    MerchantsBO mebo = new MerchantsBO();
    public string gid = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GetQeuryString("para") == "GetList")
        {
            BindData();
        }
        else if (GetQeuryString("para") == "delete")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                GroupBuyingUser g = mbo.GetModel(int.Parse(id));
                if (g != null)
                {
                    if (g.States != 2)
                    {
                        g.States = 2;
                        int num = mbo.Update(g);
                        if (num > 0)
                        {
                            try
                            {
                                GroupBuying gb = gbo.GetModel(int.Parse(g.GID.ToString()));
                                if (gb != null)
                                {
                                    string msg = new SendDX().Send(string.Format("您的团购券[{0}]已于{1}被使用。{2}", gb.Title, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), mebo.GetModel(CurrentUserInfo.MID).DX), g.Phone, 1);
                                }
                            }
                            catch (Exception)
                            {

                            }
                        }
                        Response.Write("ok");
                    }
                    else
                    {
                        Response.Write("已经使用了，无法再次使用。");
                    }
                }
               
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }
        else if (GetQeuryString("para") == "delete2")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                GroupBuyingUser g = mbo.GetModel(int.Parse(id));
                if (g != null)
                {
                    GroupBuying b = gbo.GetModel(int.Parse(g.GID.ToString()));
                    b.Places = b.Places + g.Num;
                    b.Purchases--;
                    gbo.Update(b);
                    mbo.Delete(g.ID);
                }
                Response.Write("ok");
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }
        else if (Request.QueryString["id"] != null)
        {
            gid = Request.QueryString["id"].ToString();
        }

    }

    private void BindData()
    {
        Response.Clear();
        //string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "id" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        string condition = " 1=1 ";
        string uid = txtUID.Text;// Request.Params["txtUID"] == null ? "" : Request.Params["txtUID"];
        string username = txtUserName.Text;// Request.Params["UserName"] == null ? "" : Request.Params["UserName"];
        if (uid.Trim() != "")
        {
            condition += " and uid like '%" + uid.Trim() + "%' ";
        }
        if (username.Trim() != "")
        {
            condition += " and username like '%" + username.Trim() + "%' ";
        }
        if (Request.QueryString["id"] != null)
        {
            condition += " and gid="+Request.QueryString["id"].ToString();
        }
        DataSet ds = access.GetAllBuyUser(sort, pageSize, PageNumber, condition);
        string re = ToJson.DataTableToJson(ds.Tables[0].Rows[0][0].ToString(), "rows", ds.Tables[1]);
        Response.Write(re);
        Response.End();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
}